#include <cstring>
#include <string>

#include "config.h"

using namespace std;

Config::Config() {
    idiomFeature = false;
    graphletFeature = false;
    libcallFeature = false;
    ngramFeature = false;
    dataFeature = false;

    featureListFile = NULL;

    debug = false;
}

int Config::ReadConfigFile(const string& directory) {

    
    FILE *f = fopen( (directory + string("config")).c_str(), "r");
    if (f == NULL) {
        printf("Cannot find config file!\n"); 
        return -1;
    }
    
    char option[1024];
    char str[1024];
    while (fscanf(f, "%s", option) != EOF){
        
        if (!strcmp(option, "binaryfile")) {
            fscanf(f, "%s", str);
            binaryFile = fopen( (directory + string(str)).c_str(), "r");
            if (binaryFile == NULL) {
                printf("Cannot find binaryFile %s\n", str);
                return -1;
            }
        }
        
        if (!strcmp(option, "idiom")) {
            fscanf(f, "%s", str);
            if (!strcmp(str, "yes")) {
                idiomFeature = true;
                fscanf(f, "%s", str);
                idiomFile = fopen( (directory + string(str)).c_str(), "w");
            }
        }
        
        if (!strcmp(option, "graphlet")) {
            fscanf(f, "%s", str);
            if (!strcmp(str, "yes")) {
                graphletFeature = true;
                fscanf(f, "%s", str);
                graphletFile = fopen( (directory + string(str)).c_str(), "w");
            }
        }

        if (!strcmp(option, "libcall")){
            fscanf(f, "%s", str);
            if (!strcmp(str, "yes")){
                libcallFeature = true;
                fscanf(f, "%s", str);
                libcallFile = fopen( (directory + string(str)).c_str(), "w");
            }
        }

        if (!strcmp(option, "ngram")){
            fscanf(f, "%s", str);
            if (!strcmp(str, "yes")){
                ngramFeature = true;
                fscanf(f, "%s", str);
                ngramFile = fopen( (directory + string(str)).c_str(), "w");
            }
        }

	if (!strcmp(option, "data")){
	    fscanf(f, "%s", str);
	    if (!strcmp(str, "yes")){
	        dataFeature = true;
		fscanf(f, "%s", str);
		dataFile = fopen( (directory + string(str)).c_str(), "w");
	    }
	}

	if (!strcmp(option, "addressing")) {
	    fscanf(f, "%s", str);
	    if (!strcmp(str, "yes")) {
	        addressingModeFeature = true;
		fscanf(f , "%s", str);
		addressingModeFile = fopen( (directory + string(str)).c_str(), "w" );
	    }
	}

  	if (!strcmp(option, "featurelist")){
	    fscanf(f, "%s", str);
	    if (!strcmp(str, "yes")){
	        featureList = true;
		fscanf(f, "%s", str);
		featureListFile = fopen( (directory + string(str)).c_str(), "w" );
	    }
	}
       
        if (!strcmp(option, "debug")){
            debug = true;
        }
    }
    fclose(f);
    statisticFile = fopen ( (directory + string("stat")).c_str(), "w" );
    return 0;
}

void Config::Close(){
    fclose(binaryFile);
    fclose(statisticFile);
    if (idiomFeature) fclose(idiomFile);
    if (graphletFeature) fclose(graphletFile);
    if (libcallFeature) fclose(libcallFile);
    if (ngramFeature) fclose(ngramFile);
    if (dataFeature) fclose(dataFile);
    if (addressingModeFeature) fclose(addressingModeFile);
    if (featureListFile != NULL) fclose(featureListFile);
}
